/*
 * Utiles_Busqueda.java
 *
 * Created on 27 de julio de 2007, 9:58
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package utiles;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import Cliente.*;
/**
 *
 * @author EPSGRAU
 */
public class Utiles_Busqueda {   
    
    /** Creates a new instance of Utiles_Busqueda */           
    class CambioItem implements ChangeListener{
        CambioItem( int i ){
            ind = i;
        }                
        public void stateChanged( ChangeEvent evt ) {
            indiceSel = ind;
            //System.out.println( "Cambiando estado" + indiceSel );
            
        }        
        private int ind;
    }
    
    class ClicItem implements ActionListener{
        ClicItem(){
        }
        public void actionPerformed( ActionEvent evt ){
            int index = getIndice();
            if( index != Utiles_Busqueda.INICIO_BUSQUEDA ){                
                jtf.setText( datos.get( index ).toString() );
                indEnter = true;
                //System.out.println( "Al momento de darle clic-->" + indiceSel + " " + getIndice() );
            }
        }
    }
    
    class TeclaBusqueda implements KeyListener{
        public void keyPressed( KeyEvent evt ){
        }    
        public void keyReleased( KeyEvent evt ){
            if( indActivo ){
                int tecla = evt.getKeyCode();    
                if( tecla == KeyEvent.VK_ENTER ){
                    pm.setVisible( false );
                    int index = getIndice();
                    if( index != Utiles_Busqueda.INICIO_BUSQUEDA ){
                        jtf.setText( datos.get( index ).toString() );                    
                        indEnter = true;
                        
                        return;
                    }else{
                        mostrarPopup( jtf.getText().trim() );                        
                    }
                }else if( tecla == KeyEvent.VK_ESCAPE ){
                    pm.setVisible( false );
                    reiniciaBusqueda();
                }else if( tecla != KeyEvent.VK_DOWN && tecla != KeyEvent.VK_UP ){
                    mostrarPopup( jtf.getText().trim() );
                }
                indEnter = false;
            }
        }
        public void keyTyped( KeyEvent evt ){
        }
    }
    
    class PopupListener implements PopupMenuListener {
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {            
        }
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            reiniciaBusqueda();
            //System.out.println( "Termino de hacer" + indiceSel );
        }
        public void popupMenuCanceled(PopupMenuEvent e) {
            //reiniciaBusqueda();
            //System.out.println( "Termino de hacer" + indiceSel );
        }
    }    
    
    //En el caso en que tenga almacenada las claves en un ArrayList solo las retornos
    public Utiles_Busqueda( JPopupMenu popmen, ArrayList dat, ArrayList clv ) {
        pm = popmen;
        datos = dat;
        claves = clv;
        indiceSel = Utiles_Busqueda.INICIO_BUSQUEDA;
        indiceSel = Utiles_Busqueda.INICIO_BUSQUEDA;
    }
    
    //En el caso que quiera solo el indice para obtener una clave
    public Utiles_Busqueda( JPopupMenu popmen, ArrayList dat ) {
        pm = popmen;
        datos = dat;
        indiceSel = Utiles_Busqueda.INICIO_BUSQUEDA;
    }
    
    public Utiles_Busqueda( JPopupMenu popmen, JTextField jtfCons, String cons, int[] col, ClienteRMI cli, int x, int y , String id ){
        pm = popmen;
        jtf = jtfCons;
        indiceSel = Utiles_Busqueda.INICIO_BUSQUEDA;        
        cliRMI = cli;
        indActivo = false;
        jtf.addKeyListener( new TeclaBusqueda() );
        pm.addPopupMenuListener( new PopupListener() );
        cargaBusquedaSensitiva( cons, col, id );
        this.posX = x;
        this.posY = y;                
    }
    
    public Utiles_Busqueda( JPopupMenu popmen, JTextField jtfCons, String cons, int[] col, ClienteRMI cli , String id ){
        pm = popmen;
        jtf = jtfCons;
        indiceSel = Utiles_Busqueda.INICIO_BUSQUEDA;        
        cliRMI = cli;
        indActivo = false;
        jtf.addKeyListener( new TeclaBusqueda() );
        pm.addPopupMenuListener( new PopupListener() );
        cargaBusquedaSensitiva( cons, col , id );
        this.posX = 15;
        this.posY = 20;                
    }
        
    public Utiles_Busqueda( JPopupMenu popmen, JTextField jtfCons, String cons, int[] col, ClienteRMI cli, boolean b,String id ){
        pm = popmen;
        jtf = jtfCons;
        indiceSel = Utiles_Busqueda.INICIO_BUSQUEDA;        
        cliRMI = cli;
        indActivo = b;
        jtf.addKeyListener( new TeclaBusqueda() );
        pm.addPopupMenuListener( new PopupListener() );
        cargaBusquedaSensitiva( cons, col,  id );
    }
        
    public void mostrarPopup( String cad ){
        pm.setVisible( false );
        llenaLista( cad );
        Point p = jtf.getLocation();        
        pm.show( jtf, p.x - posX, p.y - posY );        
        jtf.requestFocus();
    }
    
    public void cargaBusquedaSensitiva( String cons, int[] col, String  id){
        claves = cliRMI.getLista( cons ,id );
        datos = Utiles_DB.getArrayList( claves, col );        
    } 
        
    public void llenaLista( String bus ){
        pm.removeAll();           
        String temp = null;
        JMenuItem tempItem = null;        
        for( int i = 0; i < datos.size(); i++ ){
            temp = datos.get( i ).toString().toUpperCase();
            bus = bus.toUpperCase();
            if( temp.startsWith( bus ) ){
                tempItem = new JMenuItem( datos.get( i ).toString() );
                tempItem.addChangeListener( new CambioItem(i) );
                tempItem.addActionListener( new ClicItem() );                
                pm.add( tempItem );
            }
        }        
    }   
    
    public int getIndice(){
        //System.out.println( "Metodo de retorno-->" + indiceSel );
                        
        return indiceSel;
    }
    
    public boolean getHizoEnter(){
        return indEnter;
    }
    
    public Object getClave(){
        return claves.get( indiceSel );
    }      
    
    public Object getClave( int i ){
        return claves.get( i );        
    }
    
    public Object getDato( int i ){
        return datos.get( i );
    }
    
    public Object getSoloClave( int i ){
        return ( ( ArrayList ) this.getClave( i ) ).get( 0 );
    }
    
    public boolean getIndicadorBusqueda(){
        return indActivo;
    }
    
    public void reiniciaBusqueda(){
        indiceSel = INICIO_BUSQUEDA;
    }           
    
    public void activarBusqueda( boolean b ){
        indActivo = b;
    }
    
    public int buscaClave( String clv, int col ){        
        int index = isContenido( clv, claves, col );         
        return index;        
    }
    
    public int buscaClave( String clv ){
        return buscaClave( clv, 1 );        
    }
    
    public static int isContenido( String bus, ArrayList al, int col ){
        int pos = -1;
        ArrayList tempArr = null;
        out:
        for( int i = 0; i < al.size(); i++ ){
            tempArr = ( ArrayList ) al.get( i );
            for( int j = 0; j < tempArr.size(); j++ ){
                if( tempArr.get( col - 1 ).toString().equalsIgnoreCase( bus ) ){
                    pos = i;
                    break out;
                }
            }
        }
        return pos;
    }
    
    public static int INICIO_BUSQUEDA = -1;
    
    private JPopupMenu pm;
    private JTextField jtf;
    
    private ArrayList datos;
    private ArrayList claves;            
    private int indiceSel;
    private ClienteRMI cliRMI;      
    private boolean indActivo;
    private boolean indEnter;   //Variable que me indica si es q dio enter
    private int posX;
    private int posY;
}